import { defineStore } from 'pinia'
import { ref } from 'vue'
import { listMyTeamSpaceUsingPost } from '@/api/spaceUserController'
import { message } from 'ant-design-vue'

export const useTeamSpaceStore = defineStore('teamSpace', () => {
	const teamSpaceList = ref<API.SpaceUserVO[]>([])
	const loading = ref(false)

	/**
	 * 获取团队空间列表
	 */
	const fetchTeamSpaceList = async () => {
		loading.value = true
		try {
			const res = await listMyTeamSpaceUsingPost()
			if (res.data.code === 0 && res.data.data) {
				teamSpaceList.value = res.data.data
			} else {
				message.error('获取我的团队空间列表失败：' + res.data.message)
			}
		} catch (error) {
			console.error('获取团队空间列表失败：', error)
			message.error('获取团队空间列表失败')
		} finally {
			loading.value = false
		}
	}

	/**
	 * 刷新团队空间列表
	 */
	const refreshTeamSpaceList = async () => {
		await fetchTeamSpaceList()
	}

	/**
	 * 清空团队空间列表（用户登出时）
	 */
	const clearTeamSpaceList = () => {
		teamSpaceList.value = []
	}

	return {
		teamSpaceList,
		loading,
		fetchTeamSpaceList,
		refreshTeamSpaceList,
		clearTeamSpaceList,
	}
})
